﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace eeenno.ReaderEngine.Utilities
{
    /// <summary>
    /// Handles all application registration duties
    /// </summary>
    public static class RegistrationManager
    {
        private static string[] _app_keys = { 
                                            "pQRvv6UPB8", "pe1SDx0ITr", "Aa7F71bzp8", "0M261mjXeu", "10b2Q5L6To", "3wO79N4XMb", 
                                            "L1aJ1F7FX8", "858Bf079Jj", "ODdQhTex82", "ssbYTwXpjb", "R80S68zPaM", "9d4904RmSk",
                                            "wIE63cXwPB", "x8dBSvTz9R", "v70JAR4ETc", "O0d3n2Q1LB", "X4dS83sq39", "8dXzSvUw8T",
                                            "LTG1k91g60", "lS62rtxOeJ", "PnJ65E18R9", "Yodq2EBMr4", "JpmKXG991T"
                                            };
        private static string _trial_notifier_file = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\notifier.ini";

        public static bool IsRegistered()
        {
            return Settings.registration_key.Length > 0 && RegisterApplication(Settings.registration_key);
        }

        public static DateTime GetNotifierFileCreationTime()
        {
            if (File.Exists(_trial_notifier_file))
            {
                return File.GetCreationTime(_trial_notifier_file);
            }
            else
            {
                CreateFirstInstallFile();
                if (File.Exists(_trial_notifier_file))
                {
                    return File.GetCreationTime(_trial_notifier_file);
                }
            }
            return new DateTime(2020, 12, 31);
        }

        public static void CreateFirstInstallFile()
        {
            try
            {
                using (FileStream fs = new FileStream(_trial_notifier_file, FileMode.CreateNew))
                {
                    using (StreamWriter sw = new StreamWriter(fs))
                    {
                        sw.WriteLine("redaer deeeps");
                        sw.Close();
                    }
                }
            }
            catch (IOException e)
            {
                string ee = e.ToString();
            }
        }

        public static bool RegisterApplication(string key)
        {
            try
            {
                if (_app_keys.Contains(key))
                {
                    Settings.registration_key = key;
                    Settings.WriteSettings();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (ArgumentNullException e)
            {
                return false;
            }
        }
    }
}
